#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
sys.path.insert(0, '.')

import os
import pandas as pd

face_rec_count_file = r'../FaceRecCount.csv'
rider_gaze_count_file = r'../RiderGazeCount.csv'
device_file = r'../Device.csv'


def main():
    face_rec_count_cvs = pd.read_csv(face_rec_count_file, sep=',')
    face_rec_device_ids = face_rec_count_cvs['deviceid']
    face_rec_counts = face_rec_count_cvs['detected_count']

    rider_gaze_count_cvs = pd.read_csv(rider_gaze_count_file, sep=',')
    rider_gaze_device_ids = rider_gaze_count_cvs['deviceid']
    rider_gaze_counts = rider_gaze_count_cvs['detected_count']

    device_cvs = pd.read_csv(device_file, sep=',')
    device_ids = device_cvs['设备ID']
    xiao_qus = device_cvs['所属小区']
    xiao_qu_positions = device_cvs['小区位置']
    ai_versions = device_cvs['AI包版本号']
    onlines = device_cvs['在线情况']

    device_infos = {}
    for device_id, xiao_qus, xiao_qu_positions, ai_versions, onlines in zip(device_ids, xiao_qus, xiao_qu_positions, ai_versions, onlines):
        device_infos[device_id.strip()] = [xiao_qus.strip(), xiao_qu_positions.strip(), ai_versions.strip(), onlines.strip()]

    device_id_face_rec_counts = {}
    for device_id, count in zip(face_rec_device_ids, face_rec_counts):
        device_id = device_id.strip()
        count = int(count)
        device_id_face_rec_counts[device_id] = count

    device_id_rider_gaze_counts = {}
    for device_id, count in zip(rider_gaze_device_ids, rider_gaze_counts):
        device_id = device_id.strip()
        count = int(count)
        device_id_rider_gaze_counts[device_id] = count

    device_id_face_rec_count_rider_gaze_counts = []
    for device_id, count in device_id_rider_gaze_counts.items():
        face_rec_count = device_id_face_rec_counts.get(device_id, 0.000000001)
        device_id_face_rec_count_rider_gaze_counts.append([device_id, int(face_rec_count), count, face_rec_count*100/count])

    device_id_face_rec_count_rider_gaze_counts.sort(key=lambda x: x[-1], reverse=True)
    for device_id, face_rec_count, rider_gaze_count, ratio in device_id_face_rec_count_rider_gaze_counts:
        device_info = device_infos.get(device_id, None)
        if device_info is not None:
            # out_info = '{} {:3d} {:3d} {:.02f}% {} {} {} {}'.format(device_id, face_rec_count, rider_gaze_count, ratio, *device_info)
            out_info = '{} {:3d} {:3d} {:.02f}% {} {}'.format(device_id, face_rec_count, rider_gaze_count, ratio, device_info[0], device_info[1])
            print(out_info)
        # else:
        #     print(device_id)


if __name__ == '__main__':
    main()
